/***************************************************************
 *
 * (c) 2008 Markus Dittrich 
 *
 * This program is free software; you can redistribute it 
 * and/or modify it under the terms of the GNU General Public 
 * License Version 3 as published by the Free Software Foundation. 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License Version 3 for more details.
 *
 * You should have received a copy of the GNU General Public 
 * License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 59 Temple Place - Suite 330, 
 * Boston, MA 02111-1307, USA.
 *
 ****************************************************************/

#ifndef IO_H 
#define IO_H

/* boost include */
#include <boost/utility.hpp>

/** Qt includes */
#include <QString>
#include <QThread>
#include <QMutex>

/** STL includes */
#include <map>
#include <string>
#include <vector>

/** local includes */
#include "basic_definitions.h"
#include "data_object.h"


/****************************************************************
 * convenience typedefs
 ****************************************************************/
typedef std::pair<QString,bool> FileQueueItem;
typedef std::map<QString, DataObjectPtr> DataMap;


/****************************************************************
 *
 * IO access to the data files and reads their content 
 *
 ***************************************************************/
class IO 
  : 
    public QThread,
    public boost::noncopyable
{

  Q_OBJECT


public:

  explicit IO(QObject* parent, DataMap& data);

  /* reimplement QThreads run method */
  void run();

  /* append file(s) to the file queue */
  void append_to_filequeue(const QString& fileName);
  void append_to_filequeue(const std::vector<QString>& fileNames);
  
  /* remove file(s) from the file queue */
  void remove_from_filequeue(const QString& fileName);
  void remove_from_filequeue(const std::vector<QString>& fileNames);

  /* mark complete filequeue as unread */
  void mark_filequeue_unread();


signals:

  void currently_processed_file(QString name);
  void error_reading_file(QString name);


private:

  /* reference to main DataContainer */
  DataMap& dataContainer_;

  /* list of currently loaded files */
  mutable std::map<QString,bool> fileQueue_;

  /* mutex for serializing file access */
  QMutex mutex_;
};



#endif
